package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Jinni on 2017/7/13 0013.
 */

public class SignUpModel implements SignUpModelImpl {

    private static final String TAG = "SignUpModel";
    private OnSignUpListener mOnSignUpListener;

    public SignUpModel(OnSignUpListener onSignUpListener) {
        mOnSignUpListener = onSignUpListener;
    }

    @Override
    public void getSignUpResult(String code, String number, String name, String pass, String addTime) {
        LogUtils.i(pass, TAG);
        //构建body
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("app", HttpRequestConstant.REQUEST_APP_KEY)
                .addFormDataPart("code", code)
                .addFormDataPart("tel", number)
                .addFormDataPart("name", name)
                .addFormDataPart("pass", pass)
                .addFormDataPart("addtime", addTime)
                .build();
        Observable<CodeBean> singUp = DataManager.getSignUpResult(
                requestBody
        );
        Observable<CodeBean> signUpBeanObservable = singUp.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CodeBean>() {

            @Override
            public void onNext(@NonNull CodeBean signUpBean) {
                if (mOnSignUpListener != null) {
                    mOnSignUpListener.onSuccess(signUpBean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mOnSignUpListener != null) {
                    mOnSignUpListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getSignUpCode(String number) {
        Observable<CommonBean> singUp = DataManager.getSignUpCode(
                number
        );
        Observable<CommonBean> signUpBeanObservable = singUp.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean signUpBean) {
                if (mOnSignUpListener != null) {
                    mOnSignUpListener.onGetCodeSuccess(signUpBean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mOnSignUpListener != null) {
                    mOnSignUpListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i(TAG, "------------------ onCompleted ------------------");
            }
        });
    }

    @Override
    public void isAlreadySignUp(String number) {
        Observable<CommonBean> singUp = DataManager.isAlreadySignUp(
                number
        );
        Observable<CommonBean> signUpBeanObservable = singUp.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean signUpBean) {
                if (mOnSignUpListener != null) {
                    mOnSignUpListener.isAlreadySignUp(signUpBean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mOnSignUpListener != null) {
                    mOnSignUpListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i(TAG, "------------------ onCompleted ------------------");
            }
        });
    }

    public interface OnSignUpListener {

        void onSuccess(CodeBean signUpBean);

        void onError(Throwable throwable);

        void onGetCodeSuccess(CommonBean signUpBean);

        void isAlreadySignUp(CommonBean bean);
    }
}
